/*
 * DesktopApplication1View.java
 */
package com.catosist.run.application.desktopapplication;

import com.catosist.run.application.cliente.ManCliente;
import com.catosist.run.application.compras.ManCompras;
import com.catosist.run.application.constante.ManConstante;
import com.catosist.run.application.persona.ManPersona;
import com.catosist.run.application.rutas.ManRuta;
import com.catosist.run.application.vehiculo.ManVehiculo;
import com.catosist.run.application.venta.ManVenta;
import com.catosist.run.application.reportes.Reportes;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;

import com.catosist.run.application.viaje.ManViaje;
import com.catosist.run.business.gestorComprobanteDetalle.GestorComprobanteDetalle;
import com.catosist.run.business.gestorConstante.GestorConstante;
import com.catosist.run.business.gestorcliente.GestorCliente;
import com.catosist.run.business.gestorcolores.GestorColores;
import com.catosist.run.business.gestorcomprobante.GestorComprobante;
import com.catosist.run.business.gestorgasto.GestorGasto;
import com.catosist.run.business.gestormarca.GestorMarcas;

import com.catosist.run.business.gestorpersona.GestorPersona;
import com.catosist.run.business.gestorruta.GestorRuta;
import com.catosist.run.business.gestorvehiculo.GestorVehiculo;
import com.catosist.run.business.gestorviaje.GestorViaje;
import java.awt.Rectangle;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * The application's main frame.
 */
public class DesktopApplicationView extends FrameView {

    public DesktopApplicationView(SingleFrameApplication app) {
        super(app);

        initComponents();
        ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
        gestorViaje = (GestorViaje) ctx.getBean("gestorViaje");
        gestorPersona = (GestorPersona) ctx.getBean("gestorPersona");
        gestorRuta = (GestorRuta) ctx.getBean("gestorRuta");
        gestorVehiculo = (GestorVehiculo) ctx.getBean("gestorVehiculo");
        gestorMarcas= (GestorMarcas) ctx.getBean("gestorMarcas");
        gestorColores = (GestorColores) ctx.getBean("gestorColores");
        gestorComprobante = (GestorComprobante) ctx.getBean("gestorComprobante");
        gestorComprobanteDetalle = (GestorComprobanteDetalle) ctx.getBean("gestorComprobanteDetalle");
        gestorCliente = (GestorCliente) ctx.getBean("gestorCliente");
        gestorConstante = (GestorConstante) ctx.getBean("gestorConstante");
//        gestorComprobanteCompra = (GestorComprobanteCompra) ctx.getBean("gestorComprobanteCompra");
        gestorGasto = (GestorGasto) ctx.getBean("gestorGasto");

        gestorViaje.getAll();
        gestorPersona.getAll();
        gestorRuta.getAll();
        gestorVehiculo.getAll();
        gestorCliente.getAll();
//        gestorComprobante.getAll();


        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();

        System.out.println("resource: " + resourceMap.getResourcesDir());
        System.out.println("resource: " + resourceMap.getInteger("StatusBar.messageTimeout"));


        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {

            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String) (evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer) (evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });

        iniciarPaneles();
        System.out.println("Hi");

    }

    private void iniciarPaneles() {

        int ancho = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;
        int alto = java.awt.Toolkit.getDefaultToolkit().getScreenSize().height;

        manViaje = new ManViaje(gestorViaje, gestorRuta, gestorPersona, gestorVehiculo);
        manViaje.getPanel().setBounds(new Rectangle(ancho - 10, alto - 150));
        this.jPanelViaje.add(manViaje.getPanel());

        ManPersona manPersona = new ManPersona(gestorPersona);
        manPersona.getPanel().setBounds(new Rectangle(ancho - 10, alto - 150));
        this.jPanelPersonas.add(manPersona.getPanel());

        ManVehiculo manVehiculo = new ManVehiculo(gestorVehiculo, gestorColores, gestorMarcas);
        manVehiculo.getPanel().setBounds(new Rectangle(ancho - 10, alto - 150));
        this.jPanelVehiculo.add(manVehiculo.getPanel());

        ManRuta manRuta = new ManRuta(gestorRuta);
        manRuta.getPanel().setBounds(new Rectangle(ancho - 10, alto - 150));
        this.jPanelRuta.add(manRuta.getPanel());

        ManVenta manVenta = new ManVenta(gestorComprobante, gestorCliente, gestorViaje, gestorComprobanteDetalle, gestorConstante);
        manVenta.getPanel().setBounds(new Rectangle(ancho - 10, alto - 150));
        this.jPanelVentas.add(manVenta.getPanel());

//        ManCompras manCompras = new ManCompras(gestorComprobanteCompra);
        ManCompras manCompras = new ManCompras(gestorGasto);
        manCompras.getPanel().setBounds(new Rectangle(ancho - 10, alto - 150));
        this.jPanelCompras.add(manCompras.getPanel());

        ManCliente manCliente = new ManCliente(gestorCliente);
        manCliente.getPanel().setBounds(new Rectangle(ancho - 10, alto - 150));
        this.jPanelClientes.add(manCliente.getPanel());

        ManConstante manConstante = new ManConstante(gestorConstante);
        manConstante.getPanel().setBounds(new Rectangle(ancho - 10, alto - 150));
        this.jPanelConfiguracion.add(manConstante.getPanel());

        Reportes reportes = new Reportes(gestorComprobante, gestorGasto);
        reportes.getPanel().setBounds(new Rectangle(ancho - 10, alto - 150));
        this.jPanelReportes.add(reportes.getPanel());
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = DesktopApplication.getApplication().getMainFrame();
            aboutBox = new DesktopApplicationAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        DesktopApplication.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanelViaje = new javax.swing.JPanel();
        jPanelVehiculo = new javax.swing.JPanel();
        jPanelPersonas = new javax.swing.JPanel();
        jPanelRuta = new javax.swing.JPanel();
        jPanelVentas = new javax.swing.JPanel();
        jPanelCompras = new javax.swing.JPanel();
        jPanelClientes = new javax.swing.JPanel();
        jPanelConfiguracion = new javax.swing.JPanel();
        jPanelReportes = new javax.swing.JPanel();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();

        mainPanel.setName("mainPanel"); // NOI18N

        jTabbedPane1.setName("jTabbedPane1"); // NOI18N

        jPanelViaje.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jPanelViaje.setName("jPanelViaje"); // NOI18N

        javax.swing.GroupLayout jPanelViajeLayout = new javax.swing.GroupLayout(jPanelViaje);
        jPanelViaje.setLayout(jPanelViajeLayout);
        jPanelViajeLayout.setHorizontalGroup(
            jPanelViajeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 544, Short.MAX_VALUE)
        );
        jPanelViajeLayout.setVerticalGroup(
            jPanelViajeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 200, Short.MAX_VALUE)
        );

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(com.catosist.run.application.desktopapplication.DesktopApplication.class).getContext().getResourceMap(DesktopApplicationView.class);
        jTabbedPane1.addTab(resourceMap.getString("jPanelViaje.TabConstraints.tabTitle"), jPanelViaje); // NOI18N

        jPanelVehiculo.setName("jPanelVehiculo"); // NOI18N

        javax.swing.GroupLayout jPanelVehiculoLayout = new javax.swing.GroupLayout(jPanelVehiculo);
        jPanelVehiculo.setLayout(jPanelVehiculoLayout);
        jPanelVehiculoLayout.setHorizontalGroup(
            jPanelVehiculoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 544, Short.MAX_VALUE)
        );
        jPanelVehiculoLayout.setVerticalGroup(
            jPanelVehiculoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 200, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab(resourceMap.getString("jPanelVehiculo.TabConstraints.tabTitle"), jPanelVehiculo); // NOI18N

        jPanelPersonas.setName("jPanelPersonas"); // NOI18N

        javax.swing.GroupLayout jPanelPersonasLayout = new javax.swing.GroupLayout(jPanelPersonas);
        jPanelPersonas.setLayout(jPanelPersonasLayout);
        jPanelPersonasLayout.setHorizontalGroup(
            jPanelPersonasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 544, Short.MAX_VALUE)
        );
        jPanelPersonasLayout.setVerticalGroup(
            jPanelPersonasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 200, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab(resourceMap.getString("jPanelPersonas.TabConstraints.tabTitle"), jPanelPersonas); // NOI18N

        jPanelRuta.setName("jPanelRuta"); // NOI18N

        javax.swing.GroupLayout jPanelRutaLayout = new javax.swing.GroupLayout(jPanelRuta);
        jPanelRuta.setLayout(jPanelRutaLayout);
        jPanelRutaLayout.setHorizontalGroup(
            jPanelRutaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 544, Short.MAX_VALUE)
        );
        jPanelRutaLayout.setVerticalGroup(
            jPanelRutaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 200, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("Gestionar Rutas", jPanelRuta);

        jPanelVentas.setName("jPanelVentas"); // NOI18N

        javax.swing.GroupLayout jPanelVentasLayout = new javax.swing.GroupLayout(jPanelVentas);
        jPanelVentas.setLayout(jPanelVentasLayout);
        jPanelVentasLayout.setHorizontalGroup(
            jPanelVentasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 544, Short.MAX_VALUE)
        );
        jPanelVentasLayout.setVerticalGroup(
            jPanelVentasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 200, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab(resourceMap.getString("jPanelVentas.TabConstraints.tabTitle"), jPanelVentas); // NOI18N

        jPanelCompras.setName("jPanelCompras"); // NOI18N

        javax.swing.GroupLayout jPanelComprasLayout = new javax.swing.GroupLayout(jPanelCompras);
        jPanelCompras.setLayout(jPanelComprasLayout);
        jPanelComprasLayout.setHorizontalGroup(
            jPanelComprasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 544, Short.MAX_VALUE)
        );
        jPanelComprasLayout.setVerticalGroup(
            jPanelComprasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 200, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab(resourceMap.getString("jPanelCompras.TabConstraints.tabTitle"), jPanelCompras); // NOI18N

        jPanelClientes.setName("jPanelClientes"); // NOI18N

        javax.swing.GroupLayout jPanelClientesLayout = new javax.swing.GroupLayout(jPanelClientes);
        jPanelClientes.setLayout(jPanelClientesLayout);
        jPanelClientesLayout.setHorizontalGroup(
            jPanelClientesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 544, Short.MAX_VALUE)
        );
        jPanelClientesLayout.setVerticalGroup(
            jPanelClientesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 200, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab(resourceMap.getString("jPanelClientes.TabConstraints.tabTitle"), jPanelClientes); // NOI18N

        jPanelConfiguracion.setName("jPanelConfiguracion"); // NOI18N

        javax.swing.GroupLayout jPanelConfiguracionLayout = new javax.swing.GroupLayout(jPanelConfiguracion);
        jPanelConfiguracion.setLayout(jPanelConfiguracionLayout);
        jPanelConfiguracionLayout.setHorizontalGroup(
            jPanelConfiguracionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 544, Short.MAX_VALUE)
        );
        jPanelConfiguracionLayout.setVerticalGroup(
            jPanelConfiguracionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 200, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab(resourceMap.getString("jPanelConfiguracion.TabConstraints.tabTitle"), jPanelConfiguracion); // NOI18N

        jPanelReportes.setName("jPanelReportes"); // NOI18N

        javax.swing.GroupLayout jPanelReportesLayout = new javax.swing.GroupLayout(jPanelReportes);
        jPanelReportes.setLayout(jPanelReportesLayout);
        jPanelReportesLayout.setHorizontalGroup(
            jPanelReportesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 544, Short.MAX_VALUE)
        );
        jPanelReportesLayout.setVerticalGroup(
            jPanelReportesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 200, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab(resourceMap.getString("jPanelReportes.TabConstraints.tabTitle"), jPanelReportes); // NOI18N

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 549, Short.MAX_VALUE)
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE)
        );

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 549, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 365, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        setComponent(mainPanel);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents
    // MUESTRA TODOS LOS VIAJES
//    public void agregaPestanha(JPanel panel, String titulo) {
//        this.jTabbedPane5.add(titulo, panel);
//        this.jTabbedPane5.setSelectedIndex(this.jTabbedPane5.getTabCount() - 1);
//        ButtonTabComponent pestana = new ButtonTabComponent("nano", this.jTabbedPane5);
//        this.jTabbedPane5.setTabComponentAt(this.jTabbedPane5.getTabCount() - 1, pestana);
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanelClientes;
    private javax.swing.JPanel jPanelCompras;
    private javax.swing.JPanel jPanelConfiguracion;
    private javax.swing.JPanel jPanelPersonas;
    private javax.swing.JPanel jPanelReportes;
    private javax.swing.JPanel jPanelRuta;
    private javax.swing.JPanel jPanelVehiculo;
    private javax.swing.JPanel jPanelVentas;
    private javax.swing.JPanel jPanelViaje;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    // End of variables declaration//GEN-END:variables
    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;
    private JDialog aboutBox;
    private ManViaje manViaje;
    private GestorViaje gestorViaje;
    private GestorVehiculo gestorVehiculo;
    private GestorRuta gestorRuta;
    private GestorPersona gestorPersona;
    private GestorColores gestorColores;
    private GestorMarcas gestorMarcas;
    private GestorCliente gestorCliente;
    private GestorComprobante gestorComprobante;
    private GestorComprobanteDetalle gestorComprobanteDetalle;
    private GestorConstante gestorConstante;
//    private GestorComprobanteCompra gestorComprobanteCompra;
    private GestorGasto gestorGasto;
    private ApplicationContext ctx;
}
